package com.xuanxu.blogs.module.article.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuanxu.blogs.module.article.entity.dao.ArticleClassifyDao;
import com.xuanxu.blogs.module.article.entity.vo.admin.ArticleClassifyResponseVo;
import com.xuanxu.blogs.module.article.entity.vo.admin.InsertArticleClassifyRequestVo;
import com.xuanxu.blogs.module.article.entity.vo.admin.SelectArticleClassifyRequestVo;
import com.xuanxu.blogs.module.article.entity.vo.admin.UpdateArticleClassifyRequestVo;
import com.xuanxu.blogs.module.article.service.ArticleClassifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XuanXu
 * @version 1.0
 * @date 2024-08-21 15:33
 */
@RestController
@RequestMapping("/admin-api/article-classify")
@RequiredArgsConstructor
@Api(tags = "文章分类相关接口")
public class AdminArticleClassifyController {

    private final ArticleClassifyService articleClassifyService;

    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "currentPage", value="页码", required = true),
            @ApiImplicitParam(dataType = "Integer", name = "pageSize", value="页面大小", required = true),
    })
    @GetMapping("/page/{currentPage}/{pageSize}")
    @ApiOperation("分页查询文件分类")
    public ResponseEntity<IPage<ArticleClassifyResponseVo>> pageSelectArticleClassify(@PathVariable("currentPage") Long currentPage,
                                                                                      @PathVariable("pageSize") Long pageSize,
                                                                                      SelectArticleClassifyRequestVo selectArticleClassifyRequestVo) {
        ArticleClassifyDao articleClassifyDao = BeanUtil.copyProperties(selectArticleClassifyRequestVo, ArticleClassifyDao.class);

        IPage<ArticleClassifyDao> articleClassifyDaoPage = articleClassifyService.pageSelectArticleClassify(currentPage, pageSize, articleClassifyDao);
        IPage<ArticleClassifyResponseVo> articleClassifyResponseVoPage = new Page<>();
        BeanUtil.copyProperties(articleClassifyDaoPage, articleClassifyResponseVoPage);

        if (articleClassifyDaoPage != null) {
            List<ArticleClassifyResponseVo> articleClassifyResponseVoList = BeanUtil.copyToList(articleClassifyDaoPage.getRecords(), ArticleClassifyResponseVo.class);
            articleClassifyResponseVoPage.setRecords(articleClassifyResponseVoList);
        }

        return ResponseEntity.ok(articleClassifyResponseVoPage);
    }

    @PostMapping()
    @ApiOperation("新增文章分类")
    public ResponseEntity<Boolean> insertArticleClassify(@RequestBody InsertArticleClassifyRequestVo insertArticleClassifyRequestVo) {
        ArticleClassifyDao articleClassifyDao = BeanUtil.copyProperties(insertArticleClassifyRequestVo, ArticleClassifyDao.class);
        boolean isSuccess = articleClassifyService.insertArticleClassify(articleClassifyDao);
        return ResponseEntity.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation("根据ID修改文章分类")
    public ResponseEntity<Boolean> updateArticleClassifyById(@RequestBody UpdateArticleClassifyRequestVo updateArticleClassifyRequestVo) {
        ArticleClassifyDao articleClassifyDao = BeanUtil.copyProperties(updateArticleClassifyRequestVo, ArticleClassifyDao.class);
        boolean isSuccess = articleClassifyService.updateArticleClassifyById(articleClassifyDao);
        return ResponseEntity.ok(isSuccess);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "articleClassifyId", value = "文章分类ID", required = true)
    })
    @DeleteMapping("/{articleClassifyId}")
    @ApiOperation("根据ID删除文章分类")
    public ResponseEntity<Boolean> deleteArticleClassifyById(@PathVariable("articleClassifyId") Long articleClassifyId) {
        boolean isSuccess = articleClassifyService.deleteArticleClassifyById(articleClassifyId);
        return ResponseEntity.ok(isSuccess);
    }

    @DeleteMapping("/batch")
    @ApiOperation("批量删除文章分类")
    public ResponseEntity<Boolean> batchDeleteArticleClassifyById(@RequestBody List<Long> articleClassifyIdList) {
        boolean isSuccess = articleClassifyService.batchDeleteArticleClassifyById(articleClassifyIdList);
        return ResponseEntity.ok(isSuccess);
    }

    @GetMapping()
    @ApiOperation("查询文章分类")
    public ResponseEntity<List<ArticleClassifyResponseVo>> selectArticleClassify(SelectArticleClassifyRequestVo selectArticleClassifyRequestVo) {
        ArticleClassifyDao articleClassifyDao = BeanUtil.copyProperties(selectArticleClassifyRequestVo, ArticleClassifyDao.class);
        List<ArticleClassifyDao> articleClassifyDaoList = articleClassifyService.selectArticleClassify(articleClassifyDao);
        List<ArticleClassifyResponseVo> articleClassifyResponseVoList = BeanUtil.copyToList(articleClassifyDaoList, ArticleClassifyResponseVo.class);
        return ResponseEntity.ok(articleClassifyResponseVoList);
    }
}
